namespace FIREFLYX {
    /** 后渲染深度纹理绘制 着色器 */
    export const BUILTIN_SHADER_POSTEFF_DEPTH_TEX = `postEffDepthTex`;
    DefaultAsset.shaderGenMap[BUILTIN_SHADER_POSTEFF_DEPTH_TEX] = () => {
        const vsCode = `#version 300 es
            precision highp float;
            precision highp int;
            layout (location = 0) in vec4 a_Position; //顶点 位置 是有默认值的 (0,0,0,1)
            layout (location = 3) in vec2 a_UV;       //UV 纹理坐标
            out vec2 v_uv;
            void main() {
                v_uv = a_UV;
                vec4 pos = vec4(a_Position.xyz , 1.0);
                gl_Position = pos;
            }
        `;

        const fsCode = `#version 300 es
            precision highp float;
            precision highp int;
            uniform sampler2D u_mainTex;
            uniform float u_depthScale;
            in vec2 v_uv;
            out vec4 color;
            void main(){
                vec4 texColor = texture(u_mainTex , v_uv);    //纹理
                float depthScale = step(1.0 , u_depthScale) * u_depthScale + step(u_depthScale , 1.0) * 1.0;
                float Depth = (1.0 - texColor.r) * depthScale;   //缩放系数是为了方便观察
                // float Depth = 1.0 - texColor.r;
                color = vec4(Depth,0.0,0.0,1.0);
            }
        `;
        //glsl
        const vs = new GLSL();
        vs.setCode(vsCode);
        const fs = new GLSL();
        fs.setCode(fsCode);

        //shader
        let result: Shader = new Shader();
        result.tag = BUILTIN_SHADER_POSTEFF_DEPTH_TEX;
        result.lightMode = RenderPipeline.LM_FORWARD;
        result.vertexGLSL = vs;
        result.fragmentGLSL = fs;
        //unifrom 默认值
        result.definePropertyMap = {};

        result.definePropertyMap.u_depthScale = {
            type: "float",
            value: 1
        };

        //apply
        result.apply();

        return result;
    };
}